package com.company.cloud.goodsrestservice.service.impl;

import com.company.cloud.goodsrestservice.core.AbstractService;
import com.company.cloud.goodsrestservice.dao.SkuCommonMapper;
import com.company.cloud.goodsrestservice.model.SkuCommon;
import com.company.cloud.goodsrestservice.model.SkuOrderRel;
import com.company.cloud.goodsrestservice.service.SkuCommonService;
import com.company.cloud.goodsrestservice.service.SkuOrderRelService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


/**
 * Created by reabout on 2018/03/23.
 */
@Service
@Transactional
public class SkuCommonServiceImpl extends AbstractService<SkuCommon> implements SkuCommonService {
    @Resource
    private SkuCommonMapper skuCommonMapper;
    @Resource
    private SkuOrderRelService skuOrderRelService;

    private final Logger log = LoggerFactory.getLogger(SkuCommonService.class);

    public SkuCommon chkInsert(String materialNumber, String skuName, String skuModel, Integer stock, Integer goodsId, Integer colorId, Integer orderId) {
        SkuCommon sku = findBy("skuMaterialNumber", materialNumber);
        if (sku == null & materialNumber != "") {
            log.info("New sku_material_number {}", materialNumber);
            sku = new SkuCommon();
            sku.setSkuMaterialNumber(materialNumber);
            sku.setGoodsId(goodsId);
            sku.setColorId(colorId);
            sku.setSkuSource(1);
            sku.setSkuName(skuName);
            sku.setSkuModel(skuModel);
            sku.setSkuStock(stock);
            Long now = System.currentTimeMillis();
            sku.setCreateTime(now);
            sku.setUpdateTime(now);
            save(sku);
            sku = findBy("skuMaterialNumber", materialNumber);
            SkuOrderRel skuOrderRel = new SkuOrderRel();
            skuOrderRel.setOrderId(orderId);
            skuOrderRel.setSkuId(sku.getSkuId());
            skuOrderRelService.save(skuOrderRel);
        } else {
            if (skuName != "") {
                sku.setSkuName(skuName);
            }
            sku.setColorId(colorId);
            if (skuModel != "") {
                sku.setSkuModel(skuModel);
            }
            Long now = System.currentTimeMillis();
            sku.setUpdateTime(now);
            update(sku);
        }
        return sku;
    }

}
